package com.nuanshui.heatedloan.userinfo.service.thirdLogin;

import com.nuanshui.heatedloan.constant.WXOpenIdSource;
import com.nuanshui.framework.consts.TransactionManagerType;
import com.nuanshui.heatedloan.userinfo.constant.LoginAccountType;
import com.nuanshui.heatedloan.userinfo.dal.mapper.WeixinUserMapper;
import com.nuanshui.heatedloan.userinfo.facade.UserWeixinAccountService;
import com.nuanshui.heatedloan.userinfo.facade.thirdlogin.ThirdUserServiceConstant;
import com.nuanshui.heatedloan.userinfo.facade.thirdlogin.WeixinThirdUserService;
import com.nuanshui.heatedloan.userinfo.form.BoundUserForm;
import com.nuanshui.heatedloan.userinfo.model.ThirdUser;
import com.nuanshui.heatedloan.userinfo.model.User;
import com.nuanshui.heatedloan.userinfo.model.UserWeixinOpenId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by zxh on 2017/2/10.
 */

@Service(ThirdUserServiceConstant.WeixinUserService)
@Transactional(readOnly=true,value= TransactionManagerType.USER_INFO_TRANSACTION_MANAGER)
public class WeixinUserServiceImpl extends BaseThirdUserServiceImpl implements WeixinThirdUserService {

	private static final long serialVersionUID = -1293275475161272616L;

	@Autowired
    private WeixinUserMapper weixinUserMapper;

    @Autowired
    private UserWeixinAccountService userWeixinAccountService;

    public User loadUser( String openId) {
        return weixinUserMapper.loadUserByWeixin(openId);
    }

    @Override
    public User loadByUnionUser( String unionId) {
        return weixinUserMapper.loadUserByWeixinUnionId(unionId);
    }

    public ThirdUser loadThirdUser(String userId) {
        return weixinUserMapper.loadUserWeixin(userId);
    }

    @Transactional(value=TransactionManagerType.USER_INFO_TRANSACTION_MANAGER)
    public void insertThirdUser(BoundUserForm boundUserForm) {
    	weixinUserMapper.insertUserWeixin(boundUserForm);
    }

    @Override
    @Transactional(value=TransactionManagerType.USER_INFO_TRANSACTION_MANAGER)
    public User authUser(BoundUserForm boundUserForm)
    {
        User user = null;
        synchronized (boundUserForm.getOpenid().intern()) {
            user = weixinUserMapper.loadUserByWeixinUnionId(boundUserForm.getUnionid());

            if (user == null) {
            	weixinUserMapper.updateWeixinUnionId(boundUserForm.getOpenid(), boundUserForm.getUnionid());
                user = weixinUserMapper.loadUserByWeixinUnionId(boundUserForm.getUnionid());
                if (user != null) {
                    UserWeixinOpenId userWeixinOpenId = new UserWeixinOpenId();
                    userWeixinOpenId.setOpenId(boundUserForm.getOpenid());
                    userWeixinOpenId.setSource(WXOpenIdSource.PUBLIC_ACCOUNT);
                    userWeixinOpenId.setUserId(user.getId());
                    userWeixinOpenId.setUnionId(boundUserForm.getUnionid());
                    userWeixinAccountService.insert(userWeixinOpenId);
                }

            } else {
                User user2 = loadUser(boundUserForm.getOpenid());
                if (user2 != null && user2.getId().equals(user.getId())) {
                    user = user2;
                }
            }
            if (user == null) {// 表示第一次登陆，需要创建账号
                if (weChatService != null) {
                    boundUserForm=	weChatService.uploadThirdUserHeadImg(boundUserForm);
                }
                user =this.insertUser(boundUserForm);
                UserWeixinOpenId userWeixinOpenId = new UserWeixinOpenId();
                userWeixinOpenId.setOpenId(boundUserForm.getOpenid());
                userWeixinOpenId.setSource(WXOpenIdSource.PUBLIC_ACCOUNT);
                userWeixinOpenId.setUserId(user.getId());
                userWeixinOpenId.setUnionId(boundUserForm.getUnionid());
                userWeixinAccountService.insert(userWeixinOpenId);
            }
        }

        return user;
    }

    protected String getExistUserError()
    {
        return "该微信账号已绑定其他用户";
    }

    protected String getExistThirdUserError()
    {
        return "该微信账号已经被其他微信账号绑定";
    }

    @Override
    @Transactional(value=TransactionManagerType.USER_INFO_TRANSACTION_MANAGER)
    public void updateOpenId(String userId) {
        ThirdUser thirdUser = loadThirdUser(userId);
        weixinUserMapper.updateWeixinUserId(userId,thirdUser.getOpenid());
    }

    @Override
    @Transactional(value=TransactionManagerType.USER_INFO_TRANSACTION_MANAGER)
    public void updateWeixinUnionId(String openId, String unionId) {
        weixinUserMapper.updateWeixinUnionId(openId, unionId);
    }

    @Override
    @Transactional(value=TransactionManagerType.USER_INFO_TRANSACTION_MANAGER)
    public void deleteUser(String userId)
    {
        weixinUserMapper.deleteUserWeixin(userId);
        userWeixinAccountMapper.deleteWeixinOpenIdAccount(userId);
    }

    public int[] getMutexAccountTypes()
    {
        int accountTypes[]={LoginAccountType.QQ,LoginAccountType.WEI_BO};
        return accountTypes;
    }
}
